package com.example.nebula.util;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;

/**
 * 通用返回类
 */
@Data
public class R<T> {
    // 返回体
//    private Integer code;
//    private String msg;
//    private T data;
    private String state;
    private String errorCode;
    private String errorMessage;
    private T body;

    /**
     * 成功
     **/
    public static R success(Object object) {
        R result = new R();
        result.setErrorCode(ResultCode.SUCCESS.getCode()+"");
        result.setErrorMessage(ResultCode.SUCCESS.getMsg());
        result.setState("OK");
        result.setBody(object);
        return result;
    }

    public static <T> R<T> data(Object object) {
        R result = new R();
        result.setErrorCode(ResultCode.SUCCESS.getCode()+"");
        result.setErrorMessage(ResultCode.SUCCESS.getMsg());
        result.setBody(object);
        result.setState("OK");
        return result;
    }

//    public static <T> R<T> data(JSONObject object) {
//        R result = new R();
//        int code =object.getInteger("code");
//        if(code != 0){
//            result.setErrorCode(object.getInteger("code")+"");
//            result.setErrorMessage(object.getString("message"));
//            result.setState("ERROR");
//        }else{
//            result.setState("OK");
//            result.setBody(object);
//        }
//        return result;
//    }

    /**
     * 失败
     **/
    public static R fail(String code, String message) {
        R result = new R();
        result.setErrorCode(code);
        result.setErrorMessage(message);
//        result.setBody(object);
        result.setState("ERROR");
        return result;
    }

    public R result(Integer code, String msg, Object object) {
        R result = new R();
        result.setErrorCode(code+"");
        result.setErrorMessage(msg);
        result.setBody(object);
        return result;
    }
}

